package office.action.workflow;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import office.action.workflow.form.RemindConditionForm;
import office.action.workflow.wrapper.RemindWrapper;
import office.service.workflow.IRemindService;
import platform.common.action.BaseAction;
import platform.common.bo.Page;
import platform.common.exception.BusinessException;
import platform.util.Util;

public class RemindAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private IRemindService remindService;
	private String id;
	/**
	 * 消息提醒列表
	 */
	private List<RemindWrapper> remindlist;
	private Map<String,Long> map = new HashMap<String,Long>();;
	private RemindConditionForm condition;
	private Page page; // 分页信息
	
	public String gotoUpdateAllRemindToRead(){
		return "gotoUpdateAllRemindToRead";
	}
	
	public String gotoDeleteReadRemind(){
		return "gotoDeleteReadRemind";
	}
	
	public String gotoDeleteAllRemind(){
		return "gotoDeleteAllRemind";
	}
	
	public String gotoRemindMenu(){
		return "gotoRemindMenu";
	}
	
	public String gotoHaveReadRemind(){
		return "gotoHaveReadRemind";
	}
	
	public String deleteReadRemind(){
		try {
			if(condition == null){
				condition = new RemindConditionForm();
			}
			condition.setRemindUser(getUserId());
			remindService.deleteRemind(condition != null ?condition:null);
			this.getAjaxMessagesJson().setMessage(true, "", "删除成功");
		} catch (Exception e) {
			this.getAjaxMessagesJson().setMessage(false, "", "删除失败");
			log.error("", e);
		}
		return RESULT_AJAXJSON;
	}
	
	public String updateAllRemindToRead(){
		try {
			if(condition == null){
				condition = new RemindConditionForm();
			}
			condition.setRemindUser(getUserId());
			remindService.updateAllRemindToRead(condition != null ?condition:null);
			this.getAjaxMessagesJson().setMessage(true, "", "更新成功");
		} catch (Exception e) {
			this.getAjaxMessagesJson().setMessage(false, "", "更新失败");
			log.error("", e);
		}
		return RESULT_AJAXJSON;
	}
	
	public String haveReadRemind(){
		try {
			remindService.haveReadRemind(id);
			this.getAjaxMessagesJson().setMessage(true, "", "已阅成功");
		} catch (BusinessException e) {
			this.getAjaxMessagesJson().setMessage(false, "", "已阅失败");
			log.error("", e);
		}
		return RESULT_AJAXJSON;
	}
	
	public String getAllRemindByType() throws BusinessException{
		//加入消息提醒
		if(condition == null)
		condition=new RemindConditionForm();
		//获取当前用户
		condition.setRemindUser(getUserId());
		if(page == null){
			page = remindService.findRemindByConditionForPage(condition);
		}
		remindlist=remindService.findRemindByCondition(condition,page);
		return "getAllRemindByType";
	}
	
	public String getRemindCountByType(){
		try {
			condition=new RemindConditionForm();
			String type = Util.parsePropertiesReturnStr("project", "remind.type");
			condition.setRemindUser(getUserId());
			condition.setHandleState(0);
			String typeArray[] = type.split(",");
			for (int i = 0; i < typeArray.length; i++) {
				condition.setObjClassName(typeArray[i]);
				Long l = remindService.findRemindCountByType(condition);
				map.put(typeArray[i], l);
			}
		} catch (BusinessException e) {
			log.error("", e);
		}
		return SUCCESS;
	}
	public IRemindService getRemindService() {
		return remindService;
	}
	public void setRemindService(IRemindService remindService) {
		this.remindService = remindService;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	public List<RemindWrapper> getRemindlist() {
		return remindlist;
	}
	public void setRemindlist(List<RemindWrapper> remindlist) {
		this.remindlist = remindlist;
	}
	public RemindConditionForm getCondition() {
		return condition;
	}
	public void setCondition(RemindConditionForm condition) {
		this.condition = condition;
	}
	public Map<String, Long> getMap() {
		return map;
	}
	public void setMap(Map<String, Long> map) {
		this.map = map;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
}
